package com.ctvit.cch.web.controller;

import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.ctvit.cch.core.enums.CreationSourceEnum;
import com.ctvit.cch.core.pojo.dto.creative.InspirationCreationAddDTO;
import com.ctvit.cch.core.pojo.dto.creative.InspirationCreationMoreDTO;
import com.ctvit.cch.core.pojo.dto.creative.InspirationCreationMyDTO;
import com.ctvit.cch.core.pojo.dto.creative.InspirationCreationSubmitDTO;
import com.ctvit.cch.core.service.IInspirationCreationService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;

import java.io.IOException;
import java.io.UncheckedIOException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 灵感创作表 控制器
 *
 * @author heyingcheng
 * @date 2025/10/22
 */
@RestController
@RequestMapping("/inspiration/creation")
public class InspirationCreationController extends BaseController {

    private IInspirationCreationService inspirationCreationService;

    public InspirationCreationController(IInspirationCreationService inspirationCreationService) {
        this.inspirationCreationService = inspirationCreationService;
    }

    /**
     * 分页查询灵感创作表
     *
     * @param dto 筛选条件
     * @return {@link TableInfo} 详细信息
     */
    @PreAuthorize("@ss.hasPermi('inspiration:creation:my')")
    @PostMapping("/my")
    public TableDataInfo myList(@RequestBody @Validated InspirationCreationMyDTO dto) {
        startPage(dto.getPageNum(), dto.getPageSize());
        return getDataTable(inspirationCreationService.getMyList(dto));
    }

    /**
     * 获取灵感创作详情
     *
     * @param id 灵感创作ID
     * @return {@link AjaxResult} 灵感创作详情
     */
    @PreAuthorize("@ss.hasPermi('inspiration:creation:get')")
    @PostMapping("/get")
    public AjaxResult get(@RequestParam String id) {
        return AjaxResult.success(inspirationCreationService.get(id));
    }

    /**
     * 新增灵感创作
     *
     * @param dto 灵感创意信息
     * @return {@link AjaxResult} 新增结果
     */
    @PreAuthorize("@ss.hasPermi('inspiration:creation:add')")
    @PostMapping("add")
    public AjaxResult add(@RequestBody @Validated InspirationCreationAddDTO dto) {
        return AjaxResult.success("操作成功", inspirationCreationService.add(dto));
    }

    /**
     * 更多灵感创作
     *
     * @param dto 灵感创意信息
     * @return {@link AjaxResult} 更多结果
     */
    @PreAuthorize("@ss.hasPermi('inspiration:creation:add')")
    @PostMapping("more")
    public AjaxResult more(@RequestBody @Validated InspirationCreationMoreDTO dto) {
        return AjaxResult.success("操作成功", inspirationCreationService.more(dto));
    }

    /**
     * 删除灵感创作
     *
     * @param id 灵感创作ID
     * @return {@link AjaxResult} 删除结果
     */
    @PreAuthorize("@ss.hasPermi('inspiration:creation:delete')")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestParam String id) {
        return toAjax(inspirationCreationService.removeById(id));
    }

    /**
     * 提交灵感创作
     *
     * @param dto 灵感创作提交DTO
     * @return {@link AjaxResult} 操作结果
     * @throws IOException
     * @throws UncheckedIOException
     */
    @PreAuthorize("@ss.hasPermi('inspiration:creation:submit')")
    @PostMapping("/submit")
    public AjaxResult submit(InspirationCreationSubmitDTO dto) throws UncheckedIOException, IOException {
        if (dto.validated()) {
            if (CreationSourceEnum.INSP.name().equals(dto.getSource())) {
                return AjaxResult.success("操作成功", inspirationCreationService.submitInsp(dto.getTitle(),
                        dto.getDescription(), dto.getFileType(), dto.getFiles()));
            } else if (CreationSourceEnum.IDEA.name().equals(dto.getSource())) {
                return AjaxResult.success("操作成功", inspirationCreationService.submitIdea(dto.getRefId()));
            } else if (CreationSourceEnum.HOT.name().equals(dto.getSource())) {
                return AjaxResult.success("操作成功", inspirationCreationService.submitHot(dto.getRefId()));
            }
        }

        return AjaxResult.error("参数错误");
    }

}